﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BookStore.Solution.Entities;
using BookStore.Solution.BLL;
using BookStore.Solution.Utilities;

namespace BookStore.Solution.Presentation
{
    public partial class frmEmployee : DevComponents.DotNetBar.OfficeForm
    {
        private bool IsAdd;
        public Employee NewEmployee;
        private EmployeeManager manager = new EmployeeManager();
        public frmEmployee()
        {
            InitializeComponent();
            IsAdd = true;
        }

        public frmEmployee(Employee entity)
        {
            InitializeComponent();
            IsAdd = false;
            Text = "Cập nhật thông tin nhân viên có mã : " + entity.EmployeeID;
            btnReset.Enabled = false;
            btnSubmit.Text = "Cập nhật";
            txtEmployeeID.Enabled = false;
            NewEmployee = entity;
        }
        private void frmEmployee_Load(object sender, EventArgs e)
        {
            LoadForm();
            
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            ResetColor();
            string result = CheckValidated();
            if (result != "")
            {
                MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Employee employee = new Employee();
            employee.EmployeeID = txtEmployeeID.Text;
            employee.EmployeeName = txtEmployeeName.Text;
            employee.Birthday = dtiBirthday.Value;
            employee.Email = txtEmail.Text;
            employee.Address = txtAddress.Text;
            employee.Gender = rdMale.Checked;
            employee.PositionID = DataUtil.ToInt(cbPosion.SelectedValue);
            employee.Status = cbStatus.Text;
            employee.IndentityCard = mtbIdentityCard.Text;
            employee.PhoneNumber = mtbPhoneNumber.Text;
            if (IsAdd)
            {
                result = manager.Insert(employee);
                if (result == "Thêm thành công")
                {
                    NewEmployee = employee;
                    MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Close();
                }
                else
                {
                    MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                result = manager.Update(employee);
                if (result == "Cập nhật thành công")
                {
                    NewEmployee = employee;
                    MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Close();
                }
                else
                {
                    MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetForm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            NewEmployee = null;
            Close();
        }

        private void LoadForm()
        {
            cbStatus.Items.Add("Đang làm việc");
            cbStatus.Items.Add("Nghỉ phép");
            PositionManager positionManager = new PositionManager();
            DataTable positions = positionManager.GetAll();
            if (positions.Rows.Count == 0)
            {
                MessageBox.Show("Danh sách chức vụ rỗng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            cbPosion.DataSource = positions;
            cbPosion.DisplayMember = "PositionName";
            cbPosion.ValueMember = "PositionID";
            ResetForm();
            if (!IsAdd)
            {
                txtEmployeeID.Text = NewEmployee.EmployeeID;
                txtEmployeeID.Enabled = false;
                cbStatus.Text = NewEmployee.Status;
                txtAddress.Text = NewEmployee.Address;
                txtEmail.Text = NewEmployee.Email;
                txtEmployeeName.Text = NewEmployee.EmployeeName;
                mtbIdentityCard.Text = NewEmployee.IndentityCard;
                mtbPhoneNumber.Text = NewEmployee.PhoneNumber;
                if (NewEmployee.Gender == true)
                    rdMale.Checked = true;
                else
                    rdFemale.Checked = true;
                dtiBirthday.Value = NewEmployee.Birthday;
                cbPosion.SelectedValue = NewEmployee.PositionID;
            }
        }
        private void ResetForm()
        {
            cbStatus.SelectedIndex = 0;
            txtEmployeeID.Text = txtAddress.Text = txtEmail.Text = txtEmployeeName.Text = "";
            mtbIdentityCard.Text = mtbPhoneNumber.Text = "";
            rdMale.Checked = true;
            dtiBirthday.Value = DateTime.Parse("2/23/1994");
        }
        private string CheckValidated()
        {
            string result = "";
            if (txtEmployeeID.Text == "")
            {
                txtEmployeeID.Border.BackColor = Color.Red;
                result += "Mã nhân viên rỗng. ";
            }
            if (txtEmployeeName.Text == "")
            {
                txtEmployeeName.Border.BackColor = Color.Red;
                result += "Tên nhân viên rỗng. ";
            }
            if (mtbIdentityCard.Text == "" || mtbIdentityCard.Text.Length !=9)
            {
                result += "CMND không đúng định dạng";
                mtbIdentityCard.BackgroundStyle.BorderColor = Color.Red;
                
            }
            if (mtbPhoneNumber.Text == "" && mtbPhoneNumber.Text.Length < 9 && mtbPhoneNumber.Text.Length > 11)
            {
                result += "Số điện thoại không đúng định dạng";
                mtbPhoneNumber.BackgroundStyle.BorderColor = Color.Red;
            }
            return result;
        }

        private void ResetColor()
        {
            txtEmployeeID.Border.BackColor = StyleManager.ColorTint;
            txtEmployeeName.Border.BackColor = StyleManager.ColorTint;
            txtEmail.Border.BackColor = StyleManager.ColorTint;
            txtAddress.Border.BackColor = StyleManager.ColorTint;
            mtbIdentityCard.BackgroundStyle.BorderColor = StyleManager.ColorTint;
            mtbPhoneNumber.BackgroundStyle.BorderColor = StyleManager.ColorTint;
            dtiBirthday.BackgroundStyle.BorderColor = StyleManager.ColorTint;
        }
    }
}